package com.ls.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 登出操作
     *
     * @return
     */
    @PostMapping("sys/logout")
    public ResponseEntity<String> logout() {
        // 拿到当前jwt  从redis删掉就可以了
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        // 这个token是没有bearer
        String tokenValue = details.getTokenValue();
        Boolean flag = redisTemplate.delete("oauth:jwt:" + tokenValue);
        System.out.println(flag);
        return ResponseEntity.ok("登出成功");
    }

    @GetMapping("pay")
    @PreAuthorize("hasAuthority('delMember')")
    public String pay() {
        return "支付接口";
    }
}
